package com.sgb.kjwl.viewmodel.self.publish;

import androidx.databinding.ObservableField;

import com.sgb.kjwl.AppConstant;
import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.entity.user.AddMachineInEntity;
import com.sgb.kjwl.model.entity.user.AddressEntity;
import com.sgb.kjwl.model.entity.user.DictEntity;
import com.sgb.kjwl.repository.SelfRepertory;
import com.sgb.kjwl.view.ui.activity.self.publish.MachinePublishInActivity;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MLog;

import java.util.List;

import io.reactivex.functions.Consumer;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MachinePublishInModel extends BaseViewModel {
    public ObservableField<AddMachineInEntity> addMachineInEntity = new ObservableField<>(new AddMachineInEntity());
    private MachinePublishInActivity activity;
    private SelfRepertory repertory;

    public MachinePublishInModel(MachinePublishInActivity activity, SelfRepertory repertory) {
        this.activity = activity;
        this.repertory = repertory;
    }

    //新增机械求租发布
    public void addMachineInModel() {
        DialogHelper.showProgressDialog(activity, null, "数据提交中...", 0, false, null).setCanceledOnTouchOutside(false);
        repertory.addMachineIn(addMachineInEntity.get())
                .subscribe(new Consumer<BaseEntity>() {
                    @Override
                    public void accept(BaseEntity baseEntity) throws Exception {
                        DialogHelper.dismissProgressDialog();
                        activity.publishSuccess();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        DialogHelper.dismissProgressDialog();
                        MLog.d(AppConstant.API_LOG, "error " + throwable.getMessage());
                    }
                });
    }

    //编辑机械求租发布
    public void editMachineInModel() {
        DialogHelper.showProgressDialog(activity, null, "数提交载中...", 0, false, null).setCanceledOnTouchOutside(false);
        repertory.editMachineIn(addMachineInEntity.get())
                .subscribe(new Consumer<BaseEntity>() {
                    @Override
                    public void accept(BaseEntity baseEntity) throws Exception {
                        DialogHelper.dismissProgressDialog();
                        activity.publishSuccess();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        DialogHelper.dismissProgressDialog();
                        MLog.d(AppConstant.API_LOG, "error " + throwable.getMessage());
                    }
                });
    }

    //获取机械求租详情
    public void getMachineInDetailModel(String id) {
        repertory.getPublishMachineInDetail(id)
                .subscribe(new Consumer<BaseEntity<AddMachineInEntity>>() {
                    @Override
                    public void accept(BaseEntity<AddMachineInEntity> addMachineInEntityBaseEntity) throws Exception {
                        activity.showEditView(addMachineInEntityBaseEntity.getData());
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        DialogHelper.dismissProgressDialog();
                        MLog.d(AppConstant.API_LOG, "error " + throwable.getMessage());
                    }
                });
    }

    //获取价格单位
    public void getMachinePriceModel() {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        repertory.getMachinePrice()
                .subscribe(new Consumer<BaseEntity<List<DictEntity>>>() {
                    @Override
                    public void accept(BaseEntity<List<DictEntity>> listBaseEntity) throws Exception {
                        DialogHelper.dismissProgressDialog();
                        activity.showUnitPop(listBaseEntity.getData());
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        DialogHelper.dismissProgressDialog();
                        MLog.d(AppConstant.API_LOG, "error " + throwable.getMessage());
                    }
                });
    }

    //获取机械类型(不传获取一级分类,传一级分类no获取二级分类)
    public void getMachineTypeModel(String no) {
       /* DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        repertory.getMachineOutType()
                .subscribe(new Consumer<BaseEntity<List<PublicTypeEntity>>>() {
                    @Override
                    public void accept(BaseEntity<List<PublicTypeEntity>> listBaseEntity) throws Exception {
                        DialogHelper.dismissProgressDialog();
                        if (TextUtils.isEmpty(no)) {
                            activity.showMachineType(listBaseEntity.getData());
                        } else {
                            activity.showMachineTypeChild(listBaseEntity.getData());
                        }

                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        DialogHelper.dismissProgressDialog();
                        MLog.d(AppConstant.API_LOG, "error " + throwable.getMessage());
                    }
                });*/
    }

    public void getAddressModel() {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);

        APIManager.getInstance().getGoodsAPI().getCityList().enqueue(new Callback<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> call, Response<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> response) {
                DialogHelper.dismissProgressDialog();
                activity.showAddressPop(response.body().getData());
            }

            @Override
            public void onFailure(Call<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }
}
